package org.example.lotterysystem.service.activitystatus.operator;

import org.example.lotterysystem.dao.dataobject.ActivityUserDo;
import org.example.lotterysystem.dao.mapper.ActivityUserMapper;
import org.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.example.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{
   @Autowired
   private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
       Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatusEnum = convertActivityStatusDTO.getTargetUserStatus();
        if(null == activityId
        || CollectionUtils.isEmpty(userIds)
        || null == targetUserStatusEnum){
            return false;
        }
        List<ActivityUserDo> activityUserDoList = activityUserMapper.batchSelectByAUIds(activityId, userIds);
        if(CollectionUtils.isEmpty(activityUserDoList)){
            return false;
        }
        for (ActivityUserDo auDO : activityUserDoList){
            if(auDO.getStatus()
                    .equalsIgnoreCase(targetUserStatusEnum.name())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatusEnum = convertActivityStatusDTO.getTargetUserStatus();
    try{
        activityUserMapper.batchUpdateStatus(activityId,userIds,targetUserStatusEnum.name());
        return  true;
    }catch (Exception e){
        return false;
        }
    }
}
